package com.hanscan.gui;

import com.hanscan.util.language.LanguageConfig;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlCommandLink;
import com.icesoft.faces.component.ext.HtmlOutputLabel;
import com.icesoft.faces.component.ext.HtmlPanelGroup;
import com.icesoft.faces.component.ext.HtmlSelectOneMenu;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectionItems;
import com.icesoft.faces.component.panelstack.PanelStack;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import javax.faces.FacesException;
import com.sun.webui.jsf.model.Option;
import javax.faces.event.ActionEvent;


public class MainMenu extends AbstractPageBean
{

    private HtmlPanelGroup panelLoginOk = new HtmlPanelGroup();
    private PanelStack panelStackMainMenu = new PanelStack();
    private HtmlCommandButton panelManagementCompanies = new HtmlCommandButton();
    private HtmlPanelGroup panelGroupInit = new HtmlPanelGroup();
    private HtmlOutputLabel nameUser = new HtmlOutputLabel();
    private HtmlCommandLink commandLinkCloseSession = new HtmlCommandLink();

    final String LANGUAGE_ES = "es";
    final String COUNTRY_ES = "ES";
    final String LANGUAGE_EN = "en";
    final String COUNTRY_US = "US";
    //private boolean appletLoaded = false;
    private HtmlSelectOneMenu comboLanguages = new HtmlSelectOneMenu();
    private DefaultSelectionItems comboLanguagesDefaultItems =
            new DefaultSelectionItems();
    private HtmlOutputLabel outputLabelLanguage = new HtmlOutputLabel();
    private HtmlCommandButton btnChangeLanguage = new HtmlCommandButton();
    private HtmlCommandButton btnManagementClients = new HtmlCommandButton();
    private HtmlCommandButton btnManagementReports = new HtmlCommandButton();
    private HtmlCommandButton btnManagementSigning = new HtmlCommandButton();
    private HtmlCommandButton btnManagementTerminal = new HtmlCommandButton();
    
    
    private void _init() throws Exception
    {
        nameUser.setValue(getSessionBean1().getUser().getName()+" "+getSessionBean1().getUser().getSurname1()+
          " "+getSessionBean1().getUser().getSurname2());
        loadLanguage(getSessionBean1().getSessionBeanLanguage());
        fillLanguage();
    }

    protected void loadLanguage(LanguageConfig languageConfig)
    {
        btnChangeLanguage.setValue(languageConfig.getProperty("cambiar_idioma"));
        outputLabelLanguage.setValue(languageConfig.getProperty("idioma"));
        commandLinkCloseSession.setValue(languageConfig.getProperty("Cerrar_Sesion"));
    }

    public String commandLinkCloseSession_processAction()
    {
        getSessionBean1().setUser(null);
        return "Index";
    }

    public String btnChangeLanguage_action()
    {
        long id = Long.parseLong(comboLanguages.getValue().toString());

        if(id == 2)//2 ESPAÑOL
        {
            getSessionBean1().getSessionBeanLanguage().changeLanguage(LANGUAGE_ES, COUNTRY_ES);
        }
        else
        {
            getSessionBean1().getSessionBeanLanguage().changeLanguage(LANGUAGE_EN, COUNTRY_US);
        }
        loadLanguage(getSessionBean1().getSessionBeanLanguage());
        //TODO llamar a todos los loadLanguage de los fragmentos
        return null;
    }

     /**
     * Rellena el list box con los idiomas
     * @param
     */
    private void fillLanguage()
    {
      comboLanguagesDefaultItems.clear();
      Option optionEn = new Option(1,"English");
      Option optionEs = new Option(2,"Español");
      comboLanguagesDefaultItems.add(optionEs);
      comboLanguagesDefaultItems.add(optionEn);   
    }  

    public MainMenu()
    {
    }

    public String btnManagementClients_action(ActionEvent e)
    {
        panelStackMainMenu.setSelectedPanel("panelManagementClients");
        return null;
    }

    public String btnManagementReports_action(ActionEvent e)
    {
        panelStackMainMenu.setSelectedPanel("panelManagementCompanies");
        return null;
    }

    public String btnManagementTerminal_action(ActionEvent e)
    {
        panelStackMainMenu.setSelectedPanel("panelManagementTerminal");
        return null;
    }

    @Override
    public void init()
    {
        super.init();

        try
        {
            _init();
        } 
        catch (Exception e)
        {
            log("MainMenu Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
    }

    @Override
    public void preprocess()
    {
    }

    @Override
    public void prerender()
    {
    }

    @Override
    public void destroy()
    {
    }

///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////

    protected RequestBean1 getRequestBean1()
    {
        return (RequestBean1) getBean("RequestBean1");
    }

    protected ApplicationBean1 getApplicationBean1()
    {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    protected SessionBean1 getSessionBean1()
    {
        return (SessionBean1) getBean("SessionBean1");
    }

    public HtmlCommandButton getBtnChangeLanguage() {
        return btnChangeLanguage;
    }

    public void setBtnChangeLanguage(HtmlCommandButton btnChangeLanguage) {
        this.btnChangeLanguage = btnChangeLanguage;
    }

    public HtmlCommandButton getBtnManagementClients() {
        return btnManagementClients;
    }

    public void setBtnManagementClients(HtmlCommandButton btnManagementClients) {
        this.btnManagementClients = btnManagementClients;
    }

    public HtmlCommandButton getBtnManagementReports() {
        return btnManagementReports;
    }

    public void setBtnManagementReports(HtmlCommandButton btnManagementReports) {
        this.btnManagementReports = btnManagementReports;
    }

    public HtmlCommandButton getBtnManagementSigning() {
        return btnManagementSigning;
    }

    public void setBtnManagementSigning(HtmlCommandButton btnManagementSigning) {
        this.btnManagementSigning = btnManagementSigning;
    }

    public HtmlSelectOneMenu getComboLanguages() {
        return comboLanguages;
    }

    public void setComboLanguages(HtmlSelectOneMenu comboLanguages) {
        this.comboLanguages = comboLanguages;
    }

    public DefaultSelectionItems getComboLanguagesDefaultItems() {
        return comboLanguagesDefaultItems;
    }

    public void setComboLanguagesDefaultItems(DefaultSelectionItems comboLanguagesDefaultItems) {
        this.comboLanguagesDefaultItems = comboLanguagesDefaultItems;
    }

    public HtmlOutputLabel getOutputLabelLanguage() {
        return outputLabelLanguage;
    }

    public void setOutputLabelLanguage(HtmlOutputLabel outputLabelLanguage) {
        this.outputLabelLanguage = outputLabelLanguage;
    }

    public HtmlPanelGroup getPanelLoginOk() {
        return panelLoginOk;
    }

    public void setPanelLoginOk(HtmlPanelGroup panelLoginOk) {
        this.panelLoginOk = panelLoginOk;
    }

    public PanelStack getPanelStackMainMenu() {
        return panelStackMainMenu;
    }

    public void setPanelStackMainMenu(PanelStack panelStackMainMenu) {
        this.panelStackMainMenu = panelStackMainMenu;
    }

    public HtmlOutputLabel getNameUser() {
        return nameUser;
    }

    public void setNameUser(HtmlOutputLabel nameUser) {
        this.nameUser = nameUser;
    }

    public HtmlCommandLink getCommandLinkCloseSession() {
        return commandLinkCloseSession;
    }

    public void setCommandLinkCloseSession(HtmlCommandLink commandLinkCloseSession) {
        this.commandLinkCloseSession = commandLinkCloseSession;
    }

    public HtmlPanelGroup getPanelGroupInit() {
        return panelGroupInit;
    }

    public void setPanelGroupInit(HtmlPanelGroup panelGroupInit) {
        this.panelGroupInit = panelGroupInit;
    }

    public HtmlCommandButton getPanelManagementCompanies() {
        return panelManagementCompanies;
    }

    public void setPanelManagementCompanies(HtmlCommandButton panelManagementCompanies) {
        this.panelManagementCompanies = panelManagementCompanies;
    }

    public HtmlCommandButton getBtnManagementTerminal() {
        return btnManagementTerminal;
    }

    public void setBtnManagementTerminal(HtmlCommandButton btnManagementTerminal) {
        this.btnManagementTerminal = btnManagementTerminal;
    }

}

